﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList.Nodes;

namespace TS_XY_Train_Repaire.UserCon.Report
{
    public partial class us_Report_Field_Report_Auth_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadID;
        public us_Report_Field_Report_Auth_ManaForm()
        {
            InitializeComponent();
        }
        private void us_Report_Field_Report_Auth_ManaForm_Load(object sender, EventArgs e)
        {
            ArrayList pList = new ArrayList();
            LeverString_SysOrganization p = new LeverString_SysOrganization();
            string _str = "";
            string strSQL = "select f_id,f_name from TS_S_T_P_Report_Field where f_del=0 and f_atype=1 and f_rmid=" + _LoadID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_SysOrganization();
                    p.f_parent = _str;
                p.f_name = read["f_name"].ToString();
                p.f_id = read["f_id"].ToString();
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Field.DataSource = pList;
        }
        private void FillStuff(string _filedid)
        {
            ArrayList pList = new ArrayList();
            LeverString_SysOrganization p = new LeverString_SysOrganization();
            string _str = "";
            string strSQL = "select f_id,f_name,f_level,(select count(*) from TS_S_T_P_Report_Authority where f_del=0 and f_type=2 and f_filedid=" + _filedid+" and f_funcid=TS_S_T_P_Department.f_id) as f_auth from TS_S_T_P_Department where f_del=0 and f_id in (select f_depid from TS_S_T_P_Report_Dep where f_del=0 and f_rmid=" + _LoadID + ")";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_SysOrganization();
                if (read["f_level"].ToString() == ",")
                {
                }
                else
                {
                    _str = read["f_level"].ToString();
                    _str = _str.Substring(0, (_str).Length - 1);
                    _str = _str.Substring(_str.LastIndexOf(',') + 1);
                    p.f_pid = "D"+_str;
                }
                p.f_name = read["f_name"].ToString();
                p.f_id = "D"+read["f_id"].ToString();
                p.f_ck = Convert.ToBoolean(read["f_auth"]);
                pList.Add(p);
            }
            read.Close();
            strSQL = "select f_id,f_name,f_depid,(select count(*) from TS_S_T_P_Report_Authority where f_del=0 and f_type=0 and f_filedid=" + _filedid + " and f_funcid=TS_S_T_P_Stuff.f_id) as f_auth from TS_S_T_P_Stuff where f_del=0 and f_depid in (select f_depid from TS_S_T_P_Report_Dep where f_del=0 and f_rmid=" + _LoadID + ")";
            comm = new SqlCommand(strSQL, conn);
            read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_SysOrganization();
                p.f_id = "S" + read["f_id"].ToString();
                p.f_name = read["f_name"].ToString();
                p.f_pid = "D" + read["f_depid"].ToString();
                p.f_ck = Convert.ToBoolean(read["f_auth"]);
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Stuff.DataSource = pList;
            tl_Stuff.ExpandAll();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btok_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_S_T_P_Report_Authority set f_del=1,f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+ ",f_time=getdate() where f_del=0 and f_filedid=" + tl_Field.FocusedNode.GetValue("f_id").ToString();
            try
            {
                TS_Win_Class.MySqlCom(strSQL);
                GetNodes(tl_Stuff.Nodes);
            }
            catch
            {
            }
        }
        private void GetNodes(TreeListNodes Nodes)
        {
            string strSQL = "update TS_S_T_P_Report_Authority set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_del=0 and f_filedid=" + tl_Field.FocusedNode.GetValue("f_id").ToString();
            foreach (TreeListNode node in Nodes)
            {
                if ((bool)node.GetValue("f_ck"))
                {
                    if (node.GetValue("f_id").ToString().Substring(0, 1) == "D")
                    {
                        strSQL = "insert into TS_S_T_P_Report_Authority(f_filedid,f_funcid,f_type,f_workerid) values(" + tl_Field.FocusedNode.GetValue("f_id").ToString() + "," + node.GetValue("f_id").ToString().Substring(1) + ",2," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                    }
                    if (node.GetValue("f_id").ToString().Substring(0, 1) == "P")
                    {
                        strSQL = "insert into TS_S_T_P_Report_Authority(f_filedid,f_funcid,f_type,f_workerid) values(" + tl_Field.FocusedNode.GetValue("f_id").ToString() + "," + node.GetValue("f_id").ToString().Substring(1) + ",1," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                    }
                    if (node.GetValue("f_id").ToString().Substring(0, 1) == "S")
                    {
                        strSQL = "insert into TS_S_T_P_Report_Authority(f_filedid,f_funcid,f_type,f_workerid) values(" + tl_Field.FocusedNode.GetValue("f_id").ToString() + "," + node.GetValue("f_id").ToString().Substring(1) + ",0," + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ")";
                    }
                    TS_Win_Class.MySqlCom(strSQL);
                }
                if (node.Nodes.Count > 0)
                {
                    GetNodes(node.Nodes);
                }
            }
        }
        private void tl_Field_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                FillStuff(tl_Field.FocusedNode.GetValue("f_id").ToString());
            }
            catch
            {
                tl_Stuff.ClearNodes();
            }
        }
    }
}